/*     */ package com.zimbra.cs.account.callback;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeCallback;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.SearchAccountsOptions;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.db.DbMailbox;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DataSourceCallback
/*     */   extends AttributeCallback
/*     */ {
/*  50 */   private static final Set<String> INTERVAL_ATTRS = new HashSet();
/*     */   
/*     */   static {
/*  53 */     INTERVAL_ATTRS.add("zimbraDataSourcePollingInterval");
/*  54 */     INTERVAL_ATTRS.add("zimbraDataSourcePop3PollingInterval");
/*  55 */     INTERVAL_ATTRS.add("zimbraDataSourceImapPollingInterval");
/*  56 */     INTERVAL_ATTRS.add("zimbraDataSourceLivePollingInterval");
/*  57 */     INTERVAL_ATTRS.add("zimbraDataSourceRssPollingInterval");
/*  58 */     INTERVAL_ATTRS.add("zimbraDataSourceCaldavPollingInterval");
/*  59 */     INTERVAL_ATTRS.add("zimbraDataSourceYabPollingInterval");
/*  60 */     INTERVAL_ATTRS.add("zimbraDataSourceCalendarPollingInterval");
/*  61 */     INTERVAL_ATTRS.add("zimbraDataSourceGalPollingInterval");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void preModify(CallbackContext context, String attrName, Object attrValue, Map attrsToModify, Entry entry)
/*     */     throws ServiceException
/*     */   {
/*  72 */     if (INTERVAL_ATTRS.contains(attrName)) {
/*  73 */       String interval = (String)attrValue;
/*  74 */       if ((entry instanceof DataSource)) {
/*  75 */         validateDataSource((DataSource)entry, interval);
/*  76 */       } else if ((entry instanceof Account)) {
/*  77 */         validateAccount((Account)entry, attrName, interval);
/*  78 */       } else if ((entry instanceof Cos)) {
/*  79 */         validateCos((Cos)entry, attrName, interval);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void postModify(CallbackContext context, String attrName, Entry entry)
/*     */   {
/*  91 */     if ((!Zimbra.started()) || (!LC.data_source_scheduling_enabled.booleanValue())) {
/*  92 */       return;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  97 */     if ((context.isCreate()) && ((entry instanceof Cos))) {
/*  98 */       return;
/*     */     }
/* 100 */     if ((INTERVAL_ATTRS.contains(attrName)) || ("zimbraDataSourceEnabled".equals(attrName))) {
/*     */       try
/*     */       {
/* 103 */         if ((entry instanceof DataSource)) {
/* 104 */           scheduleDataSource((DataSource)entry);
/* 105 */         } else if ((entry instanceof Account)) {
/* 106 */           scheduleAccount((Account)entry);
/* 107 */         } else if ((entry instanceof Cos)) {
/* 108 */           scheduleCos((Cos)entry);
/*     */         }
/*     */       } catch (ServiceException e) {
/* 111 */         ZimbraLog.datasource.warn("Unable to update schedule for %s", entry, e);
/*     */       }
/* 113 */     } else if ((entry instanceof DataSource))
/*     */     {
/* 115 */       DataSourceManager.resetErrorStatus((DataSource)entry);
/*     */     }
/*     */   }
/*     */   
/*     */   private void validateDataSource(DataSource ds, String newInterval) throws ServiceException
/*     */   {
/* 121 */     Account account = ds.getAccount();
/* 122 */     if (account == null) {
/* 123 */       ZimbraLog.datasource.warn("Could not determine account for %s", new Object[] { ds });
/* 124 */       return;
/*     */     }
/* 126 */     validateInterval("zimbraDataSourcePollingInterval", newInterval, account.getAttr("zimbraDataSourceMinPollingInterval"));
/*     */   }
/*     */   
/*     */   private void scheduleDataSource(DataSource ds)
/*     */     throws ServiceException
/*     */   {
/* 132 */     Account account = ds.getAccount();
/* 133 */     if (account == null) {
/* 134 */       ZimbraLog.datasource.warn("Could not determine account for %s", new Object[] { ds });
/* 135 */       return;
/*     */     }
/* 137 */     DataSourceManager.updateSchedule(account, ds);
/*     */   }
/*     */   
/*     */   private void validateAccount(Account account, String attrName, String newInterval) throws ServiceException
/*     */   {
/* 142 */     validateInterval(attrName, newInterval, account.getAttr("zimbraDataSourceMinPollingInterval"));
/*     */   }
/*     */   
/*     */   private void scheduleAccount(Account account) throws ServiceException
/*     */   {
/* 147 */     ZimbraLog.datasource.info("Updating schedule for all DataSources for account %s.", new Object[] { account.getName() });
/* 148 */     List<DataSource> dataSources = Provisioning.getInstance().getAllDataSources(account);
/* 149 */     for (DataSource ds : dataSources) {
/* 150 */       DataSourceManager.updateSchedule(account, ds);
/*     */     }
/*     */   }
/*     */   
/*     */   private void validateCos(Cos cos, String attrName, String newInterval) throws ServiceException
/*     */   {
/* 156 */     validateInterval(newInterval, attrName, cos.getAttr("zimbraDataSourceMinPollingInterval"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void scheduleCos(Cos cos)
/*     */     throws ServiceException
/*     */   {
/* 165 */     ZimbraLog.datasource.info("Updating schedule for all DataSources for all accounts in COS %s.", new Object[] { cos.getName() });
/*     */     
/*     */ 
/* 168 */     Provisioning prov = Provisioning.getInstance();
/*     */     List<Account> accts;
/*     */     List<Account> accts;
/* 171 */     if ((prov instanceof LdapProv)) {
/* 172 */       accts = lookupAccountsFromLDAP(prov, cos.getId());
/*     */     } else {
/* 174 */       accts = lookupAccountsFromDB(prov);
/*     */     }
/*     */     
/* 177 */     for (Account account : accts) {
/* 178 */       if ((account != null) && ("active".equals(account.getAccountStatus(prov)))) {
/* 179 */         Cos accountCos = prov.getCOS(account);
/* 180 */         if ((accountCos != null) && (cos.getId().equals(accountCos.getId()))) {
/* 181 */           scheduleAccount(account);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private List<Account> lookupAccountsFromDB(Provisioning prov)
/*     */     throws ServiceException
/*     */   {
/* 190 */     Set<String> accountIds = null;
/* 191 */     List<Account> accts = new ArrayList();
/*     */     
/* 193 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 195 */       conn = DbPool.getConnection();
/* 196 */       accountIds = DbMailbox.listAccountIds(conn);
/*     */     } finally {
/* 198 */       DbPool.quietClose(conn);
/*     */     }
/*     */     
/* 201 */     for (String accountId : accountIds) {
/* 202 */       Account account = null;
/*     */       try {
/* 204 */         account = prov.get(Key.AccountBy.id, accountId);
/*     */       } catch (ServiceException e) {
/* 206 */         ZimbraLog.datasource.debug("Unable to look up account for id %s: %s", new Object[] { accountId, e.toString() });
/*     */       }
/*     */       
/* 209 */       if (account != null) {
/* 210 */         accts.add(account);
/*     */       }
/*     */     }
/*     */     
/* 214 */     return accts;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private List<Account> lookupAccountsFromLDAP(Provisioning prov, String cosId)
/*     */     throws ServiceException
/*     */   {
/* 228 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions();
/* 229 */     searchOpts.setFilter(ZLdapFilterFactory.getInstance().accountsOnServerAndCosHasSubordinates(prov.getLocalServer().getServiceHostname(), cosId));
/*     */     
/* 231 */     List accts = prov.searchDirectory(searchOpts);
/*     */     
/* 233 */     return accts;
/*     */   }
/*     */   
/*     */   private void validateInterval(String attrName, String newInterval, String minInterval) throws ServiceException
/*     */   {
/* 238 */     long interval = DateUtil.getTimeInterval(newInterval, 0L);
/* 239 */     if (interval == 0L) {
/* 240 */       return;
/*     */     }
/* 242 */     long lMinInterval = DateUtil.getTimeInterval(minInterval, 0L);
/* 243 */     if (interval < lMinInterval) {
/* 244 */       String msg = String.format("Polling interval %s for %s is shorter than the allowed minimum of %s.", new Object[] { newInterval, attrName, minInterval });
/*     */       
/*     */ 
/* 247 */       throw ServiceException.INVALID_REQUEST(msg, null);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/callback/DataSourceCallback.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */